package com.zsl.adapter;

import java.util.HashMap;
import java.util.List;
import com.zsl.demo.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
	private Context mContext;
    private LayoutInflater mInflater = null;
    private List<HashMap<String,Object>> mGroup = null;
    private List<List<HashMap<String,Object>>> mChild = null; 
    
	public ExpandableListViewAdapter(Context mContext, List<HashMap<String, Object>> mGroup,
			List<List<HashMap<String, Object>>> mChild) {
		super();
		this.mContext = mContext;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mGroup = mGroup;
		this.mChild = mChild;
	}

	
	private class GroupViewHolder {
        TextView mGroupTitle;
        TextView mGroupCount;
    }

    private class ChildViewHolder {
        TextView mChildTitle;
        TextView mChildInfo;
        LinearLayout mLayout;
    }
    
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mGroup.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mChild.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mGroup.get(groupPosition);
	}

	@Override
	public HashMap<String,Object> getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mChild.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		try
		{
			if (convertView == null) {
	            convertView = mInflater.inflate(R.layout.aaa_main_group_item, null);
	        }
	        GroupViewHolder holder = new GroupViewHolder();
	        holder.mGroupTitle = (TextView) convertView
	                .findViewById(R.id.group_item_name);
	        holder.mGroupTitle.setText(mGroup.get(groupPosition).get("grouptitle").toString());
	        holder.mGroupCount = (TextView) convertView
	                .findViewById(R.id.group_item_count);
	        holder.mGroupCount.setText("[" + mChild.get(groupPosition).size() + "]");
	        
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		try
    	{
    		if (convertView == null) {
                convertView = mInflater.inflate(R.layout.aaa_main_child_item, null);
            }
            ChildViewHolder holder = new ChildViewHolder();
    		holder.mChildTitle = (TextView) convertView.findViewById(R.id.child_item_title);
    		holder.mChildTitle.setText(getChild(groupPosition, childPosition).get("childtitle").toString());
    		holder.mChildInfo = (TextView) convertView.findViewById(R.id.offline_child_item_info);
    		holder.mChildInfo.setText("");
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
		//holder.mLayout = (LinearLayout)convertView.findViewById(R.id.offline_child_item_layout);
        return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
